package com.alex.mvppractice.news.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alex.mvppractice.R;
import com.alex.mvppractice.beans.NewsBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * ***********************************************************
 * author: alex
 * time: 16/7/19 下午2:37
 * name:
 * overview:
 * usage:
 * *************************************************************
 */
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsHolder> {
    private Context mContext;
    private List<NewsBean> mDatas;

    public NewsAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_news, parent, false);
        return new NewsHolder(view);
    }

    @Override
    public void onBindViewHolder(NewsHolder holder, int position) {
        NewsBean newsBean = mDatas.get(position);


        Picasso.with(mContext).load(newsBean.getImgsrc()).into(holder.iv_news);
        holder.tv_title.setText(newsBean.getTitle());
        holder.tv_desc.setText(newsBean.getDigest());
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    /**
     * 设置数据源
     *
     * @param datas
     */
    public void setData(List<NewsBean> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    class NewsHolder extends RecyclerView.ViewHolder {
        private ImageView iv_news;
        private TextView tv_title;
        private TextView tv_desc;

        public NewsHolder(View itemView) {
            super(itemView);
            iv_news = (ImageView) itemView.findViewById(R.id.ivNews);
            tv_title = (TextView) itemView.findViewById(R.id.tvTitle);
            tv_desc = (TextView) itemView.findViewById(R.id.tvDesc);
        }
    }
}
